package com.fiveGroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fiveGroup.mapper.OrderInfoMapper;
import com.fiveGroup.mapper.ProductMapper;
import com.fiveGroup.pojo.OrderInfo;
import com.fiveGroup.pojo.Product;
import com.fiveGroup.pojo.vo.BuyerOrderVo;
import com.fiveGroup.pojo.vo.OrderDetailVo;
import com.fiveGroup.pojo.vo.PayVo;
import com.fiveGroup.pojo.vo.SellerOrderVo;
import com.fiveGroup.service.OrderInfoService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    private final OrderInfoMapper orderInfoMapper;
    private final ProductMapper productMapper;

    public OrderInfoServiceImpl(OrderInfoMapper orderInfoMapper, ProductMapper productMapper) {
        this.orderInfoMapper = orderInfoMapper;
        this.productMapper = productMapper;
    }

    @Override
    public List<BuyerOrderVo> getBuyerOrders(Page<BuyerOrderVo> page, QueryWrapper<BuyerOrderVo> wrapper) {
        return orderInfoMapper.selectBuyerOrders(page, wrapper);
    }

    @Override
    public List<SellerOrderVo> getSellerOrders(Page<SellerOrderVo> page, QueryWrapper<BuyerOrderVo> wrapper) {
        return orderInfoMapper.selectSellerOrders(page, wrapper);
    }

    @Override
    public boolean payOrder(PayVo payVo) {
        Long orderId = payVo.getOrderId();
        String payType = payVo.getPayType();
        OrderInfo order = getById(orderId);
        if (order != null && order.getStatus() == 1) { // 1=待支付
            //TODO买家预扣款
            order.setStatus(2); // 2=待发货
            order.setPayType(payType);//支付类型
            order.setPayAt(new Date());
            return updateById(order);
        }
        return false;
    }

    @Override
    public boolean cancelOrder(OrderInfo orderInfo) {
        OrderInfo order = getById(orderInfo.getId());
        if (order != null) {
            order.setStatus(5); // 5=已取消
            updateById(order);
            Product product = productMapper.selectById(orderInfo.getProductId());
            if (product != null) {
                product.setStatus(1); //恢复商品状态为上架
                productMapper.updateById(product);
            }
            return true;
        }
        return false;
    }

    @Override
    public OrderDetailVo getOrderDetail(Long orderId) {
        return orderInfoMapper.getOrderDetail(orderId);
    }

    // 在 OrderInfoService 中添加方法
    @Override
    public int getOrderCountByDate(Date date) {
        // 这里需要根据您的具体实现来查询当天订单数量
        // 示例实现：
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(date);

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("DATE(created_at) = {0}", dateStr);
        return (int) count(queryWrapper);
    }
}